<?cs if:config.xrkmonitor_debug==1 ?>
<script src="<?cs var:config.docpath?>resource/js/FileSaver.js" type="text/javascript"></script>
<?cs else ?>
<script src="<?cs var:config.docpath?>resource/js/FileSaver.min.js?v=20200506" type="text/javascript"></script>
<?cs /if ?>

<script language="javascript" type="text/javascript">
var <?cs var:config.plugin_pre ?>_plugin_list = null;
var <?cs var:config.plugin_pre ?>_sel_plugin = null;

<?cs if:config.plugin_pre == "dpopen" ?>
var g_LocalOpenPluginList = $.parseJSON('<?cs var:config.local_open_list ?>');
<?cs /if ?>

function <?cs var:config.plugin_pre ?>SetSelplugin(selIndex)
{
	var pid;
	if(<?cs var:config.plugin_pre ?>_sel_plugin != null) {
		pid = '#<?cs var:config.plugin_pre ?>_plugin_' + <?cs var:config.plugin_pre ?>_sel_plugin.plugin_id;
		if(<?cs var:config.plugin_pre ?>_sel_plugin.plugin_id
			== <?cs var:config.plugin_pre ?>_plugin_list.list[selIndex].plugin_id) {
			return false;
		}
		$(pid).css('background-color', '#ecf2f4');
	}

	<?cs var:config.plugin_pre ?>_sel_plugin = <?cs var:config.plugin_pre ?>_plugin_list.list[selIndex];
	pid = '#<?cs var:config.plugin_pre ?>_plugin_' + <?cs var:config.plugin_pre ?>_sel_plugin.plugin_id;
	$(pid).css('background-color', '#ddf79a');
}

function <?cs var:config.plugin_pre ?>OnDownSrc()
{
    if(<?cs var:config.plugin_pre ?>_sel_plugin == null)
    {
        alertMsg.info("请右键单击插件通过弹出菜单下载");
        return;
    }

	if(typeof <?cs var:config.plugin_pre ?>_sel_plugin.local_plugin_index == 'undefined') {
		alertMsg.warn("请先安装插件");
		return;
	}
	var local = g_LocalOpenPluginList.list[<?cs var:config.plugin_pre ?>_sel_plugin.local_plugin_index];

    var requrl = "<?cs var:config.xrkmonitor_url ?>/monitor/download/plugin/public/";
	requrl += <?cs var:config.plugin_pre ?>_sel_plugin.plugin_name+ "/";
	var filename = <?cs var:config.plugin_pre ?>_sel_plugin.plugin_name+ "_";
	filename += local.ver + ".zip";
	requrl += filename;
	saveAs(requrl, filename);

	var rep_download_url = "<?cs var:config.xrkmonitor_url?>/cgi-bin/mt_slog_open?action=rep_open_download";
	rep_download_url += "&plugin_id=" + <?cs var:config.plugin_pre ?>_sel_plugin.plugin_id;
	$.ajax({ type: "get", url: rep_download_url, dataType: 'json', global: false });
}

function <?cs var:config.plugin_pre ?>OnDownConf()
{
    if(<?cs var:config.plugin_pre ?>_sel_plugin == null)
    {
        alertMsg.info("请右键单击插件通过弹出菜单下载");
        return;
    }

	if(typeof <?cs var:config.plugin_pre ?>_sel_plugin.local_plugin_index == 'undefined') {
		alertMsg.warn("请先安装插件");
		return;
	}
    var requrl = "<?cs var:config.cgipath?>mt_slog?action=down_plugin_conf";
    requrl += "&plugin_id=" + <?cs var:config.plugin_pre ?>_sel_plugin.plugin_id; 
	requrl += "&self_domain=" + window.document.domain;
    $.ajax({
        type: "get",
        url: requrl,
        dataType: 'json', 
        global: false,
        timeout: function(){
            alertMsg.warn("请求超时");
        },
        success: function(js){
            if(dmtFirstDealAjaxResponse(js))
                return;
			if(typeof js.downurl != 'undefined') {
			    saveAs(js.downurl, js.filename);
			}
            else {
                alertMsg.info("下载部署配置文件失败"); 
            }
        }
    });
}

function <?cs var:config.plugin_pre ?>_IsPluginInstall(plugin)
{
	if(g_LocalOpenPluginList.count <= 0)
		return -1;

	for(var i=0; i < g_LocalOpenPluginList.list.length; i++) {
		if(g_LocalOpenPluginList.list[i].plugin_id == plugin.plugin_id) {
			return i;
		}
	}
	return -1;
}

function <?cs var:config.plugin_pre ?>_GetPluginInstallStatus(plugin)
{
	if(g_LocalOpenPluginList.count <= 0)
		return "未安装";

	for(var i=0; i < g_LocalOpenPluginList.list.length; i++) {
		if(g_LocalOpenPluginList.list[i].plugin_id == plugin.plugin_id) {
			return "已安装";
		}
	}
	return "未安装";
}

function <?cs var:config.plugin_pre ?>_GetInstallPluginShowInfo(idx, plugin, local_plugin)
{
	var shtml = "<div plugin_type=1 style='float:left; height:240px; width:450px; background-color:#ecf2f4; margin:10px ";
	shtml += g_PluginMargin + "px; border-radius:10px; border:1px solid #000;'";
	shtml += " id='<?cs var:config.plugin_pre ?>_plugin_" + plugin.plugin_id + "'";
	if(plugin.ver != local_plugin.ver) 
		shtml += " has_new_ver=1 ";
	else
		shtml += " has_new_ver=0 ";
	shtml += " onclick='return <?cs var:config.plugin_pre ?>SetSelplugin(" + idx + ")' >";

	shtml += "<div style='float:left; height:180px; margin:10px; overflow:hidden; text-overflow:ellipsis;'>";
	shtml += "<a href='#'><img width='160px' height='180px' src='";
	if(local_plugin.pic != "" && local_plugin.pic != null) 
		shtml += '<?cs var:config.xrkmonitor_url ?>/monitor/download/plugin/'+local_plugin.pic;
	else
		shtml += '<?cs var:config.xrkmonitor_url ?>/monitor/download/plugin/plugin_def_pic.png';
	shtml += "'></img></a>"
	shtml += "</div>";

	shtml += "<div style='float:left;'>";
	shtml += "<div class='pluginName'>" + local_plugin.plugin_name + "</div>";
	shtml += "<div class='pluginItem'>安装情况: <font color='blue'>已安装(" + local_plugin.ver + ")</font>";
	if(plugin.ver != local_plugin.ver)
		shtml += "<font color='red'>&nbsp;-&nbsp;可更新</font></div>";
	else
		shtml += "</div>";

	shtml += "<div class='pluginItem'>开发语言: " + local_plugin.dev_language + "</div>";
	shtml += "<div class='pluginItem'>运行平台: " + local_plugin.dest_os + "</div>";
	if(local_plugin.set_method != 0)
		shtml += "<div class='pluginItem'>部署方式: 外置</div>";
	else
		shtml += "<div class='pluginItem'>部署方式: 内置</div>";
	shtml += "<div class='pluginItem'>安装次数: <font color='blue'>" + plugin.open_install_times + "</font></div>";
	if(plugin.ver != local_plugin.ver) {
		shtml += "<div class='pluginItem'>最新版本: <font color='red'>" + plugin.ver + "</font></div>";
		shtml += "<div class='pluginItem'>最后更新时间: <font color='red'>" + plugin.update_time + "</font></div>";
	}
	else {
		shtml += "<div class='pluginItem'>最新版本: " + plugin.ver + "</div>";
		shtml += "<div class='pluginItem'>最后更新时间: " + plugin.update_time + "</div>";
	}
	shtml += "<div class='pluginItem'>插件描述: " + local_plugin.desc.replace(/&br/g, "\r\n") + "</div>";
	shtml += "</div>";

	shtml += "<div style='width:440px; float:left; margin-left:10px;'>";
	shtml += "<img src='<?cs var:config.xrkmonitor_url ?>/monitor/themes/sexybuttons/images/icons/silk/status_online.png'>";
	shtml += "&nbsp;<font color='#255fcc'>"+local_plugin.auth+"</font></img>";
	shtml += "</div>";

	shtml += "</div>";
	return shtml;
}

function <?cs var:config.plugin_pre ?>_GetPluginShowInfo(idx, plugin)
{
	var shtml = "<div plugin_type=0 style='float:left; height:240px; width:450px; background-color:#ecf2f4; margin:10px ";
		shtml += g_PluginMargin + "px; border-radius:10px; border:1px solid #000;'";
		shtml += " id='<?cs var:config.plugin_pre ?>_plugin_" + plugin.plugin_id + "'";
		shtml += " onclick='return <?cs var:config.plugin_pre ?>SetSelplugin(" + idx + ")' >";

	shtml += "<div style='float:left; height:180px; margin:10px; overflow:hidden; text-overflow:ellipsis;'>";
	shtml += "<a href='#'><img width='160px' height='180px' src='";
	if(plugin.pic != "" && plugin.pic != null) 
		shtml += '<?cs var:config.xrkmonitor_url ?>/monitor/download/plugin/'+plugin.pic;
	else
		shtml += '<?cs var:config.xrkmonitor_url ?>/monitor/download/plugin/plugin_def_pic.png';
	shtml += "'></img></a>"
	shtml += "</div>";

	shtml += "<div style='float:left;'>";
	shtml += "<div class='pluginName'>" + plugin.plugin_name + "</div>";
	shtml += "<div class='pluginItem'>安装情况: <font color='blue'>未安装</font></div>";
	shtml += "<div class='pluginItem'>开发语言: " + plugin.dev_language + "</div>";
	shtml += "<div class='pluginItem'>运行平台: " + plugin.dest_os + "</div>";
	if(plugin.set_method != 0)
		shtml += "<div class='pluginItem'>部署方式: 外置</div>";
	else
		shtml += "<div class='pluginItem'>部署方式: 内置</div>";
	shtml += "<div class='pluginItem'>安装次数: <font color='blue'>" + plugin.open_install_times + "</font></div>";
	shtml += "<div class='pluginItem'>最新版本: " + plugin.ver + "</div>";
	shtml += "<div class='pluginItem'>更新时间: " + plugin.update_time + "</div>";
	shtml += "<div class='pluginItem'>插件描述: " + plugin.desc.replace(/&br/g, "\r\n") + "</div>";
	shtml += "</div>";

	shtml += "<div style='width:440px; float:left; margin-left:10px;'>";
	shtml += "<img src='<?cs var:config.xrkmonitor_url ?>/monitor/themes/sexybuttons/images/icons/silk/status_online.png'>";
	shtml += "&nbsp;<font color='#255fcc'>"+plugin.auth+"</font></img>";
	shtml += "</div>";

	shtml += "</div>";
	return shtml;
}


function <?cs var:config.plugin_pre ?>SetpluginList()
{
	if(<?cs var:config.plugin_pre ?>_plugin_list == null 
			|| <?cs var:config.plugin_pre ?>_plugin_list.count <= 0)
		return ;

	var ulist = <?cs var:config.plugin_pre ?>_plugin_list.list;
	dmtSetPluginMarginInfo();

	var uhtml = "";
	var j = 0;
	for(var i=0; i < ulist.length; i++) {
		j = <?cs var:config.plugin_pre ?>_IsPluginInstall(ulist[i]);
		if(j >= 0) {
			ulist[i].local_plugin_index = j;
			g_LocalOpenPluginList.list[j].remote_plugin_index = i;
			uhtml += <?cs var:config.plugin_pre ?>_GetInstallPluginShowInfo(i, ulist[i], g_LocalOpenPluginList.list[j]);
		}
		else {
			uhtml += <?cs var:config.plugin_pre ?>_GetPluginShowInfo(i, ulist[i]);
		}
	}

	$("#<?cs var:config.plugin_pre ?>_plugin_list").html(uhtml);
	$("#<?cs var:config.plugin_pre ?>_plugin_list").children().hover(
		function(){
			$(this).css({"margin-top":"6px", "border-top-width":"5px", "border-color":"blue"});
			$(this).find('.pluginName').css('font-size', '20px');
		}, 
		function(){
			$(this).css({"margin-top":"10px", "border-top-width":"1px", "border-color":"#000"});
			$(this).find('.pluginName').css('font-size', '18px');
		}
	);

	$("#<?cs var:config.plugin_pre ?>_plugin_list").children().each(function() {
		var sMenu = 'mypluginOpenUnIns';
		if($(this).attr('plugin_type') == 1) {
			if($(this).attr('has_new_ver') == 0)
				sMenu = 'mypluginOpenIns';
			else
				sMenu = 'mypluginOpenInsUp';
		}
		$(this).contextMenu(sMenu, {
			callback: function(t, op) {
				t.trigger('click');
			},
			bindings: { 
				downconf:function(t, m) { 
					return <?cs var:config.plugin_pre ?>OnDownConf();
				},
				downall:function(t, m) { 
					return <?cs var:config.plugin_pre ?>OnDownSrc();
				},
				showinfo:function(t, m) { 
					return <?cs var:config.plugin_pre ?>ShowInfo();
				},
				showinfo_up:function(t, m) { 
					return <?cs var:config.plugin_pre ?>ShowInfoUp();
				},
				update:function() {
					return <?cs var:config.plugin_pre ?>UpdatePlugin();
				},
				install:function(t, m) { 
					return <?cs var:config.plugin_pre ?>InstallPlugin();
				}
			}
		});
	});
}

function <?cs var:config.plugin_pre ?>UpdatePlugin()
{
	if(<?cs var:config.plugin_pre ?>_sel_plugin == null)
	{
		alertMsg.info("请右键单击插件通过弹出菜单安装");
		return;
	}

	var requrl = "<?cs var:config.xrkmonitor_url?>/cgi-bin/mt_slog_open?action=open_get_plugin_pb_info";
	requrl += "&plugin_id=" + <?cs var:config.plugin_pre ?>_sel_plugin.plugin_id; 
	$.ajax({
		type: "get",
		url: requrl,
		dataType: 'json', 
		global: false,
		timeout: function(){
			alertMsg.warn("请求超时");
		},
		success: function(js){
			if(dmtFirstDealAjaxResponse(js))
				return;
			if(js.has_pb_info == 0) {
				alertMsg.info("插件更新信息获取失败 !");
				return;
			}
			js.plugin_id = <?cs var:config.plugin_pre ?>_sel_plugin.plugin_id;
			js.dev_language = <?cs var:config.plugin_pre ?>_sel_plugin.dev_language;
			js.dest_os = <?cs var:config.plugin_pre ?>_sel_plugin.dest_os;
			if(typeof <?cs var:config.plugin_pre ?>_sel_plugin.pic != "undefined"
				&& <?cs var:config.plugin_pre ?>_sel_plugin.pic.pic != "")
			{
				js.plugin_pic = <?cs var:config.plugin_pre ?>_sel_plugin.pic;
			}
			<?cs var:config.plugin_pre ?>UpdatePluginPb(js, <?cs var:config.plugin_pre ?>_sel_plugin);
		}
	});
}

function <?cs var:config.plugin_pre ?>UpdatePluginPb(pbJs, psel)
{
	var in_url = "<?cs var:config.cgipath?>mt_slog?action=update_open_plugin";
	var plugin_info = JSON.stringify(pbJs);

	$.ajax({
		type: "post",
		url: in_url,
		data: { plugin: JSON.stringify(pbJs) },
		dataType: 'json', 
		global: false,
		timeout: function(){
			alertMsg.warn("请求超时");
		},
		success: function(js){
			if(dmtFirstDealAjaxResponse(js))
				return;
			if(js.ret != 0) {
				alertMsg.info("插件更新失败: " + js.msg);
				return;
			}
			else {
				alertMsg.correct("插件更新成功, 插件部署方法请查看帮助文档");
				return navTab.reload();
			}
		}
	});
}

function <?cs var:config.plugin_pre ?>InstallPlugin()
{
	if(<?cs var:config.plugin_pre ?>_sel_plugin == null)
	{
		alertMsg.info("请右键单击插件通过弹出菜单安装");
		return;
	}

	var requrl = "<?cs var:config.xrkmonitor_url?>/cgi-bin/mt_slog_open?action=open_get_plugin_pb_info";
	requrl += "&plugin_id=" + <?cs var:config.plugin_pre ?>_sel_plugin.plugin_id; 
	$.ajax({
		type: "get",
		url: requrl,
		dataType: 'json', 
		global: false,
		timeout: function(){
			alertMsg.warn("请求超时");
		},
		success: function(js){
			if(dmtFirstDealAjaxResponse(js))
				return;
			if(js.has_pb_info == 0) {
				alertMsg.info("插件安装信息获取失败 !");
				return;
			}
			js.plugin_id = <?cs var:config.plugin_pre ?>_sel_plugin.plugin_id;
			js.dev_language = <?cs var:config.plugin_pre ?>_sel_plugin.dev_language;
			js.dest_os = <?cs var:config.plugin_pre ?>_sel_plugin.dest_os;
			if(typeof <?cs var:config.plugin_pre ?>_sel_plugin.pic != "undefined"
				&& <?cs var:config.plugin_pre ?>_sel_plugin.pic.pic != "")
			{
				js.plugin_pic = <?cs var:config.plugin_pre ?>_sel_plugin.pic;
			}
			<?cs var:config.plugin_pre ?>InstallPluginPb(js, <?cs var:config.plugin_pre ?>_sel_plugin);
		}
	});
}

function <?cs var:config.plugin_pre ?>InstallPluginPb(pbJs, psel)
{
	var in_url = "<?cs var:config.cgipath?>mt_slog?action=install_open_plugin";
	var plugin_info = JSON.stringify(pbJs);

	$.ajax({
		type: "post",
		url: in_url,
		data: { plugin: JSON.stringify(pbJs) },
		dataType: 'json', 
		global: false,
		timeout: function(){
			alertMsg.warn("请求超时");
		},
		success: function(js){
			if(dmtFirstDealAjaxResponse(js))
				return;
			if(js.ret != 0) {
				alertMsg.info("插件安装失败: " + js.msg);
				return;
			}
			else {
				alertMsg.correct("插件安装成功, 插件部署方法请查看帮助文档");
				var rep_install_url = "<?cs var:config.xrkmonitor_url?>/cgi-bin/mt_slog_open?action=rep_open_install";
				rep_install_url += "&plugin_id=" + js.plugin_id;
				$.ajax({ type: "get", url: rep_install_url, dataType: 'json', global: false });
				navTab.reload();
			}
		}
	});
}

function <?cs var:config.plugin_pre ?>ShowInfo()
{
	if(<?cs var:config.plugin_pre ?>_sel_plugin == null)
	{
		alertMsg.info("请先点击插件列表选择一个要查看的插件");
		return;
	}

	var url = "<?cs var:config.cgipath?>mt_slog?action=show_local_mt_plugin&plugin_pre=<?cs var:config.plugin_pre ?>";
	url += "&plugin_id=" + <?cs var:config.plugin_pre ?>_sel_plugin.plugin_id; 

	var op = $.parseJSON('{"mask":true,"maxable":false,"height":600,"width":920}'); 
	$.pdialog.open(url, "<?cs var:config.plugin_pre ?>_dlg_show_plugin", "本地已安装监控插件详情", op); 
}

function <?cs var:config.plugin_pre ?>ShowInfoUp()
{
	if(<?cs var:config.plugin_pre ?>_sel_plugin == null)
	{
		alertMsg.info("请先点击插件列表选择一个要查看的插件");
		return;
	}

	var url = "<?cs var:config.cgipath?>mt_slog?action=show_mt_plugin&plugin_pre=<?cs var:config.plugin_pre ?>";
	url += "&plugin_id=" + <?cs var:config.plugin_pre ?>_sel_plugin.plugin_id; 

	var op = $.parseJSON('{"mask":true,"maxable":false,"height":600,"width":920}'); 
	$.pdialog.open(url, "<?cs var:config.plugin_pre ?>_dlg_show_plugin", "查看监控插件", op); 
}

function <?cs var:config.plugin_pre ?>Search(pglobal)
{
	var requrl = '<?cs var:config.xrkmonitor_url ?>/cgi-bin/mt_slog_open';
	var reqdata = new Object();
	reqdata.action = 'open_get_plugins';
	reqdata.rhost = location.host;
	reqdata.dev_language = $('#<?cs var:config.plugin_pre ?>_SearchBar select[name=dev_language]').val();
	reqdata.dest_os = $('#<?cs var:config.plugin_pre ?>_SearchBar select[name=dest_os]').val();
	reqdata.open_src = $('#<?cs var:config.plugin_pre ?>_SearchBar select[name=open_src]').val();
	reqdata.set_method = $('#<?cs var:config.plugin_pre ?>_SearchBar select[name=set_method]').val();

	$.ajax({
		type:'post',
		url: requrl,
		data: reqdata,
		global: pglobal,
		dataType: 'json',
		success: function(js) {
			if(js.ret != 0) {
				alertMsg.warn("监控插件信息获取失败:" + js.errmsg);
				return;
			}
			$("#<?cs var:config.plugin_pre ?>_plugin_list").html('');
			if(js.count > 0) {
				<?cs var:config.plugin_pre ?>_plugin_list = js;
				<?cs var:config.plugin_pre ?>SetpluginList();
			}
		}
	});
	return false;
}

$(document).ready(function(){
	<?cs var:config.plugin_pre ?>Search(false);
});

</script>

<style type="text/css">
.pluginName { font-size:18px; font-weight:bold; margin-top:10px; overflow:hidden; text-overflow:ellipsis; }
.pluginItem { font-size:12px; width:260px; overflow:hidden; text-overflow:ellipsis; }
</style>

<div class="pageHeader" style="border:1px #B8D0D6 solid">
	<div class="searchBar">
		<ul class="searchContent" id='<?cs var:config.plugin_pre ?>_SearchBar'>
			<li>
				<label>开发语言:</label>
				<select name="dev_language">
					<option value="all">-全部-</option>
					<option value="c/c++">c/c++</option>
					<option value="php">php</option>
					<option value="linux shell">linux shell</option>
					<option value="javascript">javascript</option>
					<option value="java">java</option>
					<option value="python">python</option>
				</select>
			</li>

			<li>
				<label>运行平台:</label>
				<select name="dest_os"> 
					<option value="all">-全部-</option>
					<option value="Linux">Linux</option>
					<option value="Windows">Windows</option>
				</select>
			</li>

			<li>
				<label>是否开源:</label>
				<select name="open_src"> 
					<option value="999">-全部-</option>
					<option value="1">开 源</option>
					<option value="0">闭 源</option>
				</select>
			</li>

			<li>
				<label>部署方式:</label>
				<select name="set_method">
					<option value="999">-全部-</option>
					<option value="0">内 置</option>
					<option value="1">外 置</option>
				</select>
			</li>
		</ul>

		<div class="subBar">
			<ul>
				<li>
					<button type="button" class="buttonActive" onclick='<?cs var:config.plugin_pre ?>Search(true);'><i class='icon-search icon-large'></i>查找</button></li>
			</ul>
		</div>
	</div>
</div>

<div class="pageContent">
	<div layoutH="0" width='100%' id="<?cs var:config.plugin_pre ?>_plugin_list">
	</div> 
</div>

